<template>
  <view class="photo" :style="[{ backgroundColor: color,height:list.length>0?'auto':'100vh' }]">
    <cu-custom bgColor="bgcolorfff" :isBack="true">
      <block slot="content" class="toptext">
        <text>{{ allData.title || "" }}</text>
      </block>
    </cu-custom>

    <!-- 顶部轮播图 -->
    <view class="playvideo">
      <video
        object-fit="fill"
        :src="videoMeta.fileUrl"
        :poster="videoMeta.mainImage"
        :enable-progress-gesture="true"
        :autoplay='true'
      ></video>
    </view>

    <!-- 主文章区域 -->
    <view class="article">
      <text class="article-title">{{ allData.title || "" }}</text>
      <view class="article-icon">
        <image :src="allData.category.icon" mode="widthFix" class="tuji" />
        <text>{{ allData.category.name || "" }}</text>
      </view>
      <text class="article-text">
        {{ allData.summary || "" }}
      </text>
    </view>

    <!-- 评论缩小版 -->
    <view class="dis">
      <view class="discuss">
        <view class="discuss_list" @click="openComlist">
          <view v-for="(item, index) in previewList" :key="index">
            <text>{{ item.userName }}:</text>
            <text class="discuss_list_margin">{{ item.intro }}</text>
          </view>
        </view>
        <view class="speakEvery" @click="switchIn = false">说点什么...</view>
      </view>
    </view>

    <!-- 底部图文推荐列表 -->
    <view
      class="recommend"
      v-if="list.length > 0"
      :style="[{ borderTop: `14px solid #f8f8f8` }]"
    >
      <text class="recommend-title">相关视频推荐</text>
      <!-- 视频盒子 -->
      <view class="video_tulist">
        <view
          class="video_box"
          v-for="(item, index) in list"
          :key="index"
          @click="jump(item)"
        >
          <view
            class="left_img"
            :style="{ backgroundImage: `url(${item.covers[0].fileUrl})` }"
          >
          </view>
          <view class="icon_videotime" v-if="item.type == 5">
            <text>0:03:00</text>
          </view>
          <view class="video_right_text">
            <text>{{ item.title }}</text>
            <view class="video_right_bo">
              <image :src="item.category.icon" mode="widthFix" />
              <text>{{ item.category.name }}</text>
            </view>
          </view>
        </view>
      </view>

      <view class="kongTab"></view>
    </view>
    <view class="kongTab"></view>
    <!-- 底部点赞兰 -->
    <evaluate
      v-model="switchIn"
      :height="safeArea"
      @likeArtice="likeArtice"
      :likeIndex="likeArticeValue"
      :evaluateState="evaluateState"
      @openCommlist="offList = true"
    ></evaluate>
    <!-- 弹出的真实的输入框 -->
    <reallyInput
      @down="down"
      :switchIn="switchIn"
      v-model="reallyValue"
      @submite="submite"
    ></reallyInput>

    <!-- 评论详细列表 -->
    <commentList
      v-model="offList"
      :AllSC="safeArea"
      ref="commentList"
      :discussList="discussList"
      :hasChart="hasChart"
      @scrollBottom="scrollBottom"
    ></commentList>
    <image
      :src="
        'https://static.77du.net/template/ciff/img/hartgif.gif?id=' +
        Math.random()
      "
      @click.stop
      v-if="showLIkeGif"
      class="showLikeGift"
      style="width: 750rpx; height: 750rpx;"
    />
    <loading v-if="loadingflag"></loading>
    <Prompt v-model="authSwitch"></Prompt>
  </view>
</template>

<script>
import cardlist from "../../conpoments/cardlist";
import evaluate from "../../conpoments/evaluate";
import reallyInput from "../../conpoments/reallyInput";
import commentList from "../../conpoments/commentList";

import waterfall from "../../conpoments/waterfall";
import Pullcard from "../../conpoments/pullcard";

import {
  inspectHasColl,
  likeArticle,
  delectLike,
  userTalk,
  replyMsg,
  photoArticle,
  getVideoUrl,
  recommendNew,
  getCommentlistData,
} from "../../api/api";
export default {
  data() {
    return {
      switchIn: true,
      currentIndex: 1,
      list: [],
      reallyValue: "",
      offList: false, //控制评论详细列表的弹窗
      safeArea: "", //安全区域高度
      likeArticeValue: 0,
      id: "", //视频id
      allData: {}, //所有数据
      showLIkeGif: false,
      evaluateState: {
        favorite: {},
        commentNum: "", //评论的数量
      },
      videoMeta: {}, //视频信息
      discussList: [], //所有的评论数据
      index: 0, //评论列表的页码
      size: 10, //每条评论的数量
      hasChart: true, //还有数据吗
      scw: "",
      HeightArr: [],
    };
  },
  async onShow() {
    this.safeArea = this.$model;
    this.scw = uni.getSystemInfoSync().windowWidth * 2;
    try {
      //底部推荐数据
      let res = await recommendNew(this.id);
      if (res[1].data.code === "000000") {
        const { list } = res[1].data;
        console.log(list);
        this.list = list;
      }
    } catch (error) {}
    this.getcomData(true);
  },
  components: {
    cardlist,
    evaluate,
    reallyInput,
    commentList,
    waterfall,
    Pullcard,
  },
  watch: {
    switchIn: function (newvalue) {
      if (newvalue == true) {
        this.openChartItem = "";
      }
    },
  },
  onShareAppMessage(res) {
    return {
      title: this.allData.shareTitle,
      path: "/pages/photo/photo?id=" + this.id,
      imageUrl: this.allData.shareImage,
      // desc: this.evaluateState.shareSummary,
    };
  },
  async onLoad({ id }) {
    this.id = id; //当前视频文章的id
    try {
      this.loadingflag = true;
      let res = await photoArticle(this.id);
      this.loadingflag = false;
      if (res[1].data.code === "000000") {
        this.allData = res[1].data.data;
        const { favorite, commentNum } = res[1].data.data;
        favorite.isFavorite == true
          ? (this.likeArticeValue = 1)
          : (this.likeArticeValue = 0); //是否收藏过
        this.evaluateState.favorite = favorite;
        this.evaluateState.favorite.favoriteNumber = Number(
          this.evaluateState.favorite.favoriteNumber
        );
        this.evaluateState.commentNum = commentNum;
        // 组织输入框的数据
      }
      if (this.allData.media && this.allData.media.playId != null) {
        let resvideo = await getVideoUrl(this.allData.media.playId);
        if (resvideo[1].data.code === "000000") {
          this.videoMeta = resvideo[1].data.data;
        }
      }
    } catch (error) {}
  },
  computed: {
    previewList() {
      // 文章中部的聊天的三条数据
      if (this.discussList.length > 0) {
        let s = this.discussList.length > 3 ? 3 : this.discussList.length;
        let arr = [];
        for (let index = 0; index < s; index++) {
          arr.push(this.discussList[index]);
        }
        return arr;
      } else {
        return 0;
      }
    },
    height() {
      return (index) => {
        if (this.list.length == this.HeightArr.length) {
          return this.HeightArr[index].height + "rpx";
        } else {
          return "260rpx";
        }
      };
    },
    color() {
      if (this.list.length > 0) {
        return "#fcfcfc";
      } else {
        return "#fff";
      }
    },
  },
  methods: {
    //真实输入框切换
    down(flag) {
      this.switchIn = flag;
    },
    openComlist() {
      this.offList = true;
      this.$refs.commentList.startfocus = false;
    },
    async likeArtice(e) {
      console.log("like", this.evaluateState.favorite.favoriteNumber);
      if (!this.AuthState()) return;
      this.likeArticeValue = e;
      if (e == 1) {
        this.evaluateState.favorite.favoriteNumber += 1;
        this.showLIkeGif = true;
        setTimeout(() => {
          this.showLIkeGif = false;
        }, 2125);
        let res = await likeArticle(this.id);
        if (res[1].data.code === "000000") {
        }
      } else {
        if (!Number(this.evaluateState.favorite.favoriteNumber) == 0) {
          this.evaluateState.favorite.favoriteNumber -= 1;
        }
        //取消收藏
        let res2 = await delectLike(this.id);
        if (res2[1].data.code === "000000") {
          uni.showToast({
            title: "取消收藏",
            duration: 800,
            icon: "none",
          });
        }
      }
    },
    loadimg(e, index) {
      const { height, width } = e.detail;
      this.HeightArr.push({
        index: index,
        height: (this.scw * height) / width,
      });
    },
    //输入框提交评论或者回复评论
    async submite(e) {
      console.log("发送了");
      if (!this.AuthState()) return;
      let res = await userTalk(this.id, this.reallyValue);
      if (res[1].data.code == "000000") {
        this.switchIn = e;
        this.reallyValue = "";
        this.flashChart()
        uni.showToast({
          title: "评论成功,等待审核",
          duration: 1500,
          icon: "none",
        });
      } else {
        uni.showToast({
          title: "评论失败",
          duration: 1500,
          icon: "none",
        });
      }
    },
    //刷新评论数据
    flashChart() {
      this.index = 0;
      this.discussList = [];
      this.hasChart = true;
      this.getcomData();
    },
    jump(item) {
      let url = "";
      if (item.type == 2) {
        //图文
        url = "/pages/article/article?id=" + item.id;
      } else if (item.type == 3) {
        //图集
        url = "/pages/photo/photo?id=" + item.id;
      } else if (item.type == 5) {
        //视频
        url = "/pages/video/video?id=" + item.id;
      } else if (item.type == 7) {
        //专题
        url = "/pages/picAtlas/picAtlas";
      } else if (item.type == 6) {
        //排行榜
        url = "/pages/crunchies/crunchies";
      } else if (item.type == 1) {
        return;
      }
      uni.navigateTo({
        url,
      });
    },
    async getcomData(first = false) {
      this.index++;
      try {
        let res = await getCommentlistData(this.index, this.size, this.id);
        if (res[1].data.code === "000000") {
          const { content } = res[1].data.data;
          console.log(content);
          if (content != null && content != [] && content.length > 0) {
            this.discussList.push(...(content || []));
          } else {
            this.hasChart = false;
          }
          console.log(res);
          // this.discussList=
        }
      } catch (error) {}
    },
    //评论列表滚动到底部加载
    scrollBottom() {
      console.log("触底了");
      if (this.hasChart) {
        this.getcomData();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.photo {
  background-color: #ffffff;
  -webkit-overflow-scrolling: touch;
}

.article {
  padding-left: 40rpx;
  padding-right: 43rpx;
  padding-top: 45rpx;
  background-color: #ffffff;
  .article-title {
    font-size: 34rpx;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: rgba(36, 36, 36, 1);
    line-height: 38rpx;
  }

  .article-icon {
    margin: 35rpx 0;
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .tuji {
      width: 26rpx;
    }

    > text {
      &:last-child {
        font-size: 24rpx;
        font-family: Source Han Sans CN;
        font-weight: 300;
        color: rgba(117, 117, 117, 1);
        line-height: 38rpx;
        margin-left: 12rpx;
      }
    }
  }

  .article-text {
    font-size: 30rpx;
    font-family: PingFang SC;
    font-weight: 400;
    color: rgba(36, 36, 36, 1);
    line-height: 48rpx;
    width: 100%;
    text-align: justify;
    line-height: 48rpx;
    text-align: justify;
    letter-spacing: 3rpx;
    word-break: break-word;
  }
}

.dis {
  padding: 0 40rpx;
  margin-top: 50rpx;
  padding-bottom: 64rpx;

  .discuss {
    background: rgba(248, 248, 248, 1);
    border-radius: 10rpx;
    margin: 0 auto;
    padding-left: 30rpx;
    padding-top: 28rpx;
    padding-bottom: 33rpx;

    .speakEvery {
      padding-top: 9rpx;
      font-size: 28rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: rgba(117, 117, 117, 1);
    }

    .discuss_list {
      display: flex;
      justify-content: flex-start;
      flex-direction: column;
      align-items: flex-start;

      .discuss_list_margin {
        margin-left: 16rpx;
      }

      > view {
        padding-bottom: 23rpx;

        text {
          &:first-child {
            font-size: 24rpx;
            font-family: PingFang SC;
            font-weight: bold;
            color: rgba(36, 36, 36, 1);
          }

          &:last-child {
            font-size: 24rpx;
            font-family: PingFang SC;
            font-weight: 500;
            color: rgba(117, 117, 117, 1);
          }
        }
      }
    }
  }
}

.recommend {
  // margin-top: 14px;
}

.recommend-title {
  display: inline-block;
  width: 100%;
  font-size: 28rpx;
  font-family: Source Han Sans CN;
  font-weight: 600;
  color: rgba(36, 36, 36, 1);
  padding-left: 38rpx;
  display: block;
  padding-bottom: 55rpx;
  padding-top: 43rpx;
}

.waterfall_box {
  // padding-top: 32rpx;
  width: 100vw;

  .waterfall_box_cen {
    margin-top: 23rpx;
  }
}

.kongTab {
  height: 98rpx;
}

.likeThisArtice {
  position: fixed;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 9999999;
}

.playvideo {
  width: 100%;
  max-height: 505rpx;

  > video {
    width: 100%;
  }
}

.video_tulist {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  width: 100%;
  padding-left: 30rpx;
  padding-right: 36rpx;

  .video_box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
    height: auto;
    position: relative;

    .left_img {
      width: 320rpx;
      height: 200rpx;
      background-repeat: no-repeat;
      background-size: cover;
      border-radius: 14rpx;
      flex-shrink: 0;
    }

    .icon_videotime {
      position: absolute;
      background: rgba(43, 43, 43, 0.5);
      border-radius: 6rpx;
      padding: 8rpx 12rpx;
      text-align: center;
      left: 225rpx;
      bottom: 14rpx;
      display: flex;
      justify-content: center;
      align-items: center;

      > text {
        font-size: 16rpx;
        font-family: PingFang SC;
        font-weight: 400;
        color: rgba(255, 255, 255, 1);
      }
    }

    .video_right_text {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: flex-start;
      height: 200rpx;
      margin-left: 20rpx;

      > text {
        font-size: 26rpx;
        font-family: PingFang SC;
        font-weight: bold;
        color: rgba(36, 36, 36, 1);
        line-height: 38rpx;
        margin-top: -6rpx;
      }
    }

    .video_right_bo {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      margin-bottom: -3rpx;

      > image {
        width: 27rpx;
        vertical-align: bottom;
      }

      > text {
        font-size: 24rpx;
        font-family: PingFang SC;
        font-weight: 400;
        color: rgba(117, 117, 117, 1);
        margin-left: 12rpx;
      }
    }
  }
}
.toptext {
  width: 100%;
  display: inline-block;
  font-family: PingFang SC;
  color: rgba(36, 36, 36, 1);
  text-align: center;
}
</style>
